#include "ArmorDetector.h"

namespace hitcrt {
bool ArmorDetector::detect(const Frame& frame, const Color color,
                           const ROI& roi, BarDetectorBase& barDetector,
                           ArmorAssemblerBase& assembler,
                           std::vector<Armor>& armors) {
    std::vector<hitcrt::LightBar> lightbars;
   
    preProcess(color, frame.image());
    if (!barDetector.detect(roi, m_binImg, m_enemyChannel, m_selfChannel,
                            lightbars)) {
        return false;
    }
    if (!assembler.assemble(lightbars, m_srcImg, armors)) {
        return false;
    }

    return true;
}

/**
 * @brief 二值化预处理得到需要通道信息
 * @author LiuZhihao (2488288779@qq.com)
 */
bool ArmorDetector::preProcess(const Color color, const cv::Mat& srcImg) {
    /// TODO 需要填写///
    cv::Mat single_channel[3];
    // #pragma omp parallel for num_threads(12)
    cv::split(srcImg, single_channel);

    if (color == RED) {
        m_enemyChannel = single_channel[0];
        m_selfChannel = single_channel[1];
    } else {
        m_enemyChannel = single_channel[1];
        m_selfChannel = single_channel[0];
    }
    m_srcImg = srcImg;
    cv::threshold(m_enemyChannel, m_binImg, m_binaryThreshold, 255,
                  cv::THRESH_BINARY);
    return true;
}
}  // namespace hitcrt